/**
 * 后端接口模块
 */
import axios from "axios";

// 设置基础地址
// const path1 = 'http://121.199.53.78:8889'
// const path1 = 'http://localhost:8889'
const path1 = '/api1'

/* 设置拦截器 */
axios.interceptors.request.use(config => {
   config.method = config.method.toUpperCase()
   config.withCredentials = true // 允许跨域请求时发送cookie
   return config
})
axios.interceptors.response.use(response => {
   return response.data
}, error => {
   return error
})

/* -----------------------------------用户信息-------------------------------------*/
/* 登录操作 */
function toLogin(data) {
   return axios.post(path1 + '/user/login', data)
}
/* 注册用户操作 */
function registerUser(data) {
   return axios.post(path1 + '/user/register', data)
}
function userCookieLogin(username) {
   return axios.post(path1 + '/user/cookieLogin', {username})
}
/* 保存注册用户的详细信息 */
function saveBaseUserInfo(obj) {
   return axios.post(path1 + '/user/inituserinfo', obj)
}
/* 退出登陆 */
function logoOut() {
   return axios.post(path1 + '/user/logout')
}
/* 更新用户信息 */
function updateUserBaseInfo(updateInfoObj) {
   return axios.post(path1 + '/user/updateuserinfo', updateInfoObj)
}

/* -----------------------------------商家列表-------------------------------------*/
/* 获取首页商家列表数据 */
function getshoplist() {
   return axios.get(path1 + '/shop/shoplist')
}
/* 获取指定商家的商品数据 */
function getGoodsById(shopId) {
   return axios.get(path1 + '/shop/goods?id=' + shopId)
}
/* 根据id获取商家信息 */
function getShopInfo(shopId) {
   return axios.get(path1 + '/shop/info?id=' + shopId)
}

/* -----------------------------------商家评论-------------------------------------*/
/* 获取商家评论数据 */
function getComments(shopId) {
   return axios.get(path1 + '/comment/getcommentsbyid?id=' + shopId)
}
/* 新增一条评论 */
function insertComment(obj) {
   return axios.post(path1 + '/comment/indertcomment', obj)
}
/* 评论一条订单 */
function commentOrder(orderId) {
   return axios.get(path1 + '/order/commentorder?orderId=' + orderId)
}

/* -----------------------------------用户地址-------------------------------------*/
/* 获取用户收货地址 */
function getUserAddressList(username) {
   return axios.get(path1 + '/address/addresslist?username=' + username)
}
/* 新增一条用户的收货地址 */
function insertOneAddress(address) {
   return axios.post(path1 + '/address/insertaddress', address)
}
/* 更新一条用户的收货地址 */
function updateOneAddress(new_address) {
   return axios.post(path1 + '/address/updateaddress', new_address)
}
/* 删除一条用户的收货地址 */
function delOneAddress(address_id) {
   return axios.post(path1 + '/address/deladdress', { address_id })
}

/* -----------------------------------用户订单-------------------------------------*/
/* 获取用户订单 */
function getOrders(username) {
   return axios.get(path1 + '/order/getorders?username=' + username)
}
/* 新增一条订单 */
function insertOrder(order) {
   return axios.post(path1 + '/order/insertorder', order)
}
/* 删除一条订单 */
function delOrder(orderId) {
   return axios.post(path1 + '/order/delorder', { orderId })
}
/* 完成一条订单 */
function finishOneOrder(orderId) {
   return axios.post(path1 + '/order/finishorder', { orderId })
}
/* 根据id获取订单详情 */
function getOrderDetailById(orderId) {
   return axios.post(path1 + '/order/getorderdetailbyid', { orderId })
}

/* -----------------------------------搜素-------------------------------------*/
function getpwdShops(pwd) {
   return axios.get(path1 + '/search/getpwdshop?pwd=' + pwd)
}

// 向外暴露
export {
   toLogin,
   userCookieLogin,
   registerUser,
   logoOut,
   updateUserBaseInfo,
   saveBaseUserInfo,

   getshoplist,
   getGoodsById,
   getShopInfo,

   insertComment,
   getComments,

   getUserAddressList,
   insertOneAddress,
   updateOneAddress,
   delOneAddress,

   getOrders,
   insertOrder,
   delOrder,
   finishOneOrder,
   getOrderDetailById,
   commentOrder,

   getpwdShops
}